#!/bin/bash
 
# 启动Topic测试并保存进程PID
echo "Starting Topic Test..."
current_dir=$(pwd)
export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
export FASTRTPS_DEFAULT_PROFILES_FILE=${current_dir}/dds_config.xml

python3 publisher.py > /dev/null 2>&1 &
PUB_PID=$!
python3 subscriber.py > topic_test.log 2>&1 &
SUB_PID=$!
 
# 启动Latency测试并保存进程PID
echo "Starting Latency Test..."
python3 latency_pub.py > /dev/null 2>&1 &
LPUB_PID=$!
python3 latency_sub.py > latency_test.log 2>&1 &
LSUB_PID=$!
 

# 设置超时定时器
echo "Tests will run for $TEST_DURATION minutes..."

# 初始化变量
elapsed_time=0        # 已过去的时间（秒）
total_duration=3600   # 总测试时长（秒，60分钟）
 
# 循环执行测试
while [ $elapsed_time -lt $total_duration ]; do
    sleep 60         # 休眠1分钟
    elapsed_time=$((elapsed_time + 60))
    
    # 输出已测试时间和剩余时间
    remaining_time=$((total_duration - elapsed_time))
    echo "已测试时间: $((elapsed_time / 60)) 分钟，剩余时间: $((remaining_time / 60)) 分钟"
done
 
echo "测试完成！"

# 关闭所有测试进程
echo "Stopping all tests..."
kill $PUB_PID $SUB_PID $LPUB_PID $LSUB_PID 2>/dev/null
wait $PUB_PID $SUB_PID $LPUB_PID $LSUB_PID 2>/dev/null
 
echo "All tests completed and processes terminated."
